package com.example.demo.utils;

import com.example.demo.Config.HBaseConfig;
import com.example.demo.pojo.DataPojo;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.RandomRowFilter;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author WZH
 * @create 2021-10-24 18:17
 * @desc hbase工具类
 **/
@Slf4j
public class HBaseUtils {

    private static Admin admin =HBaseConfig.getAdmin();
    private static Connection connection =HBaseConfig.getConnection();

    /**
     * 判断表是否存在
     * * @return {{@link null}}
     * @author WZH
     * @date 2021/9/14 11:00
     */
    public static boolean isTableExist(String tableName){
        try {
            return admin.tableExists(TableName.valueOf(tableName));
        } catch (IOException e) {
            e.printStackTrace();
            log.warn(tableName+"不存在");
            return false;
        }
    }

    /**
     *
     * @param tableName
     * @param columnFamily
     * @return {}
     * @author WZH
     * @date 2021/9/14 11:05
     */
    public  static void createTable(String tableName, String... columnFamily) throws
            MasterNotRunningException, ZooKeeperConnectionException, IOException{

        //判断表是否存在
        if(isTableExist(tableName)){
            System.out.println("表" + tableName + "已存在");
        }else{
            //创建表属性对象,表名需要转字节
            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
            //创建多个列族
            for(String cf : columnFamily){
                descriptor.addFamily(new HColumnDescriptor(cf));
            }
            //根据对表的配置，创建表
            admin.createTable(descriptor);
            System.out.println("表" + tableName + "创建成功！");
        }
    }

    /**
     * 插入一行数据
     * @param tableName 表名
     * @param rowKey 行键
     * @param columnFamily 列簇
     * @param column 列名
     * @param value 值
     * @return {}
     * @author WZH
     * @date 2021/9/14 11:14
     */
    public static synchronized void  addRowData(String tableName, String rowKey, String columnFamily, String
            column, String value) throws IOException{
        //创建Table对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        //向表中插入数据
        Put put = new Put(Bytes.toBytes(rowKey));
        //向Put对象中组装数据
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
//      添加数据到table
        table.put(put);
        table.close();
    }



    public static void main(String[] args) {
        System.setProperty("hadoop.home.dir", "E:\\test");
//        System.setProperty("hadoop.home.dir", "E:\\test");
//        boolean data_table = HBaseUtils.isTableExist("DATA_TABLE");
//        System.out.println(data_table);


        try {
            HBaseUtils.createTable("Students","info");
        } catch (IOException e) {
            e.printStackTrace();
        }


//        try {
//            HBaseUtils.addRowData("DATA_TABLE", "WD001", "WD_DATA", "value", "20");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


    }
}
